<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}用户`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="账号" prop="account">
        <el-input v-model="drawerProps.row!.account" placeholder="请填写账号" clearable></el-input>
      </el-form-item>
      <el-form-item v-if="drawerProps.row!.id == null || drawerProps.row!.id == undefined" label="登录密码" prop="password">
        <el-input v-model="drawerProps.row!.password" type="password" />
      </el-form-item>
      <el-form-item label="用户名称" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写用户名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-switch
          v-model="drawerProps.row!.status"
          :active-value="1"
          :inactive-value="0"
          @change="handleChangeStatus(drawerProps.row.status, drawerProps.row.id)"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input v-model="drawerProps.row!.mobile" placeholder="请填写手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="memo">
        <el-input v-model="drawerProps.row!.memo" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive } from "vue";
// import { userStatus } from "@/utils/dict";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";

const rules = reactive({
  account: [{ required: true, message: "请填写账号" }],
  password: [{ required: true, message: "请填写密码" }],
  name: [{ required: true, message: "请填写用户名" }]
});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      // await drawerProps.value.api!(drawerProps.value.row); // { ...params, user_id: params.id }
      await drawerProps.value.api!({ ...drawerProps.value.row, user_id: drawerProps.value.row.id }); //
      ElMessage.success({ message: `${drawerProps.value.title}用户成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
const emit = defineEmits(["changeStatus"]);
const handleChangeStatus = (status: number | string | undefined, id: number | undefined) => {
  emit("changeStatus", { status, id });
};
defineExpose({
  acceptParams
});
</script>
